<?php
/**
 * Org_relation 组织关系图
 */
apf_require_class("Bll_BLL");
class Bll_Attendance_OrgRelation extends BLL {


    /**
     * 获取主管id,org_relation 表
     * 支持取单个上级 传参 int level 以及多个上级 传参 array level
     * @retun user_id or array('level'=>user_id)
     */
    public function get_sup_id($user_id,$level = 0) {
        $this->session_act();
        $rows = $this->get_orgrelation_dao()->find(array(
                'user_id' => $user_id
        ));
        if(empty($rows)) {
            return false;
        }
        $rs = array();
        foreach($rows as $v) {
            $data[$v['level']] = $v['superior_id'];
        }
        if(is_int($level) && $level >0){
            $rs = $data[$level];
        }elseif(is_array($level) && !empty($level)){
            foreach ($level as $val){
                if($data[$val]){
                    $rs[$val] = $data[$val];
                }
            }
        }else{
            $rs = $data;
        }
        return $rs;
    }


   /**
    * 获取上级信息
    * 支持取单个上级 传参 int ids 以及多个上级 传参 array ids
    * @retun array ($level => $name)
    */
    public function get_sup_name($ids){
        if(is_string($ids) && $ids >0){
            $data = $this->get_userinfo_dao()->find_by_id($ids);
            $rs = $data['name'];
        }elseif(is_array($ids) && !empty($ids)){
            $data = $this->get_userinfo_dao()->find_by_ids($ids);
            foreach($ids as $level => $user_id){
                $rs[$level] = $data[$user_id]['name'];
                ksort($rs);
            }
        }else{
            return false;
        }
        return $rs;
    }

    /**
     * 得到下属的信息
     * @param unknown_type $user_id
     */
    public function get_subordinate_by_user($user_id,$level = 1,$order = 'user_id asc') {
        $result = array();
        if(empty($user_id)) {
            return $result;
        }

        $where = array('superior_id'=>$user_id);
        if(!empty($level)) {
            $where['level'] = $level;
        }
        $result = $this->get_orgrelation_dao()->find_assoc($where,$order);
        return $result;
    }

    /**
     * 得到下属的信息
     * 根据user_id 通过find by where
     * @param unknown_type $user_id
     */
    public function get_subordinate_by_user_id($user_id,$level = 1,$order = 'user_id asc') {
        $result = array();
        if(empty($user_id)) {
            return $result;
        }

        $where = array('superior_id'=>$user_id);
        if(!empty($level)) {
            $where['level'] = $level;
        }
        $result = $this->get_orgrelation_dao()->find($where,$order);
        return $result;
    }

    /**
     * @return DAO_Attendance_AdminRole
     */
    private function get_role_dao() {
        $dao = $this->get_dao('DAO_Attendance_AdminRole');
        return $dao;
    }

    /**
     *
     * @return Dao_Attendance_Apps
     */
    private function get_app_dao() {
        $dao = $this->get_dao('Dao_Attendance_Apps');
        return $dao;
    }

    /**
     *
     * @return DAO_Attendance_LeaveRecord
     */
    private function get_leaverecord_dao() {
        $dao = $this->get_dao('DAO_Attendance_LeaveRecord');
        return $dao;
    }

    /**
     *
     * @return Dao_Attendance_OrgRelation
     */
    private function get_orgrelation_dao() {
        $dao = $this->get_dao('Dao_Attendance_OrgRelation');
        return $dao;
    }

    /**
     * return DAO_Attendance_UserRole
     */
    private function get_user_role_dao(){
        return $this->get_dao('DAO_Attendance_UserRole');
    }

    /**
     * return DAO_Attendance_UserInfo
     */
    private function get_userinfo_dao(){
        return $this->get_dao('DAO_Attendance_UserInfo');
    }
}
